package com.myc.oss.controller;

import com.myc.oss.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @Value("${storage.service.type}")
    private String storageType;

    @GetMapping("/testGetAllBuctet")
    public String testGetAllBuctet() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);

    }

    @GetMapping("/testBuctet")
    public String testBuctet() throws Exception {
        return storageType;

    }

    /***
     * 获取url
     * @param bucket
     * @param objectName
     * @return
     */
    @RequestMapping("/getUrl")
    public String getUrl(String bucket,String objectName){
        return fileService.getUrl(bucket,objectName);
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        objectName = objectName + "/" +uploadFile.getOriginalFilename();
        return fileService.upload(uploadFile,bucket,objectName);
    }
}
